import os

import openpyxl
# from common.operateWebdriver import OperateWebdrive
# from common.factory import Factory
# from common.operateExcel import OperateExcel
from common.processingTestData import ProcessingTestData




common_data = {"common_input": [
    {'id': 2, 'result': None, 'keyword': '输入', 'type': 'id', 'locator': 'kw', 'index': None, 'input': '天风少年',
     'check': None, 'time': None},
    {'id': 3, 'result': None, 'keyword': '隐式等待', 'type': None, 'locator': None, 'index': None, 'input': None,
     'check': None, 'time': 3},
    {'id': 4, 'result': None, 'keyword': '等待元素可见', 'type': 'id', 'locator': 'kw', 'index': None, 'input': None,
     'check': None, 'time': 3}],
    "common_xxx": [{'id': 1, 'result': None, 'keyword': '打开网页', 'type': 'url', 'locator': 'www.qq.com', 'index': None,
                    'input': None,
                    'check': None, 'time': None}], }
nomal_data = {"baidu": [
    {'id': 1, 'result': None, 'keyword': '打开网页', 'type': 'url', 'locator': 'https://www.baidu.com', 'index': None,
     'input': None, 'check': None, 'time': None},
    {'id': 2, 'result': None, 'keyword': '等待元素可见', 'type': 'xpath', 'locator': '//*[@id="kw"]', 'index': None,
     'input': None, 'check': None, 'time': 3},
    {'id': 3, 'result': None, 'keyword': '调用用例', 'type': None, 'locator': 'common_input', 'index': None,
     'input': None, 'check': None, 'time': None},
    {'id': '#4', 'result': None, 'keyword': '关闭浏览器', 'type': None, 'locator': None, 'index': None, 'input': None,
     'check': None, 'time': None},
    {'id': 4, 'result': None, 'keyword': '关闭浏览器', 'type': None, 'locator': None, 'index': None, 'input': None,
     'check': None, 'time': None}
],
    "baidu222": [
        {'id': 1, 'result': None, 'keyword': '打开网页', 'type': 'url', 'locator': 'https://www.baidu.com', 'index': None,
         'input': None, 'check': None, 'time': None},
        {'id': 2, 'result': None, 'keyword': '等待元素可见', 'type': 'xpath', 'locator': '//*[@id="kw"]', 'index': None,
         'input': None, 'check': None, 'time': 3},
        {'id': 3, 'result': None, 'keyword': '调用用例', 'type': None, 'locator': 'common_input', 'index': None,
         'input': None, 'check': None, 'time': None},
        {'id': '4', 'result': None, 'keyword': '关闭浏览器', 'type': None, 'locator': None, 'index': None, 'input': None,
         'check': None, 'time': None}]
}


def init_common_case(self, case):
    """
    :param case:{id:xx,keyword:xx,...}
    :return:[{id:xx,keyword:xx,...},{id:xx,keyword:xx,...}]
    """
    if case['keyword'] == '调用用例':
        ptd = ProcessingTestData()
        sheet_name = case['locator']  # 公共用例对应的sheet页名称
        isOK, result = ptd.get_common_case(sheet_name)  # result --->[{id:xx,keyword:xx,...},{id:xx,keyword:xx,...}]

        if isOK:
            for i in result:
                isOK, result_1 = self.init_common_case(i)  # 递归检查公共用例里是否存在调用用例 result_1 --->[{id:xx,keyword:xx,...},{id:xx,keyword:xx,...}]
                if isOK:
                    index = result[i] #‘调用用例’在数组中的下标
                    for j in result_1:
                        result.insert(index+1,j) #在‘调用用例’后追加数据
                        index +=1

                else:
                    return isOK,result_1

        return isOK,result




a = [{'keyword':'a1','locator':'a1'},{'keyword':'调用用例','locator':'cc'},{'keyword':'a3','locator':'a3'}]
cc = [{'keyword':'c1','locator':'c1'},{'keyword':'c2','locator':'c2'},{'keyword':'调用用例1','locator':'dd'}]
dd = [{'keyword':'d1','locator':'d1'},{'keyword':'d2','locator':'d2'},{'keyword':'d3','locator':'d3'}]
ee = [{'keyword':'e1','locator':'e1'},{'keyword':'e2','locator':'e2'},{'keyword':'e3','locator':'e3'}]



def aaaa(cases):
    for i in cases:
        if i['keyword']=='调用用例':
            index = cases.index(i)
            x = i['locator']
            com = ['cc', 'dd']
            if x in com:
                for j in cc:
                    cases.insert(index+1,j)
                    index +=1
    return cases
xxxx = [{'keyword':'a1','locator':'a1'},{'keyword':'调用用例','locator':'cc'},{'keyword':'a3','locator':'a3'}]
aaaa(xxxx)
print(xxxx)

# data = [{"keyword": "打开网页", "type": "url", "locator": "https://www.baidu.com", "index": "", "input": "", "check": ""},
#         {"keyword": "等待元素可见", "type": "id", "locator": "kw", "index": "", "input": "", "check": ""},
#         {"keyword": "输入", "type": "id", "locator": "kw", "index": "", "input": "python", "check": ""},
#         {"keyword": "点击", "type": "xpath", "locator": "//input[@type='submit']", "index": "", "input": "", "check": ""},
#         {"keyword": "等待", "type": "", "locator": "3", "index": "", "input": "", "check": ""},
#         {"keyword": "关闭浏览器", "type": "", "locator": "", "index": "", "input": "", "check": ""},
#
#         # {"type": "xpath", "locator": "//*[@id='kw']", "index": "", "input": ""},
#         # {"": "", "locator": "", "index": "", "input": ""}
#
#         ]

